//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FDVanillaEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, UInt64 timeSteps, UInt64 gridPoints, Microsoft::FSharp::Core::FSharpOption<Boolean>^ timeDependent) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFDVanillaEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Size _timeSteps = (QuantLib::Size)ValueHelper::Convert (timeSteps);
        QuantLib::Size _gridPoints = (QuantLib::Size)ValueHelper::Convert (gridPoints);
        bool _timeDependent = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (timeDependent) ? (bool)ValueHelper::Convert (timeDependent->Value) : false); //4
        _ppFDVanillaEngine = new boost::shared_ptr<QuantLib::FDVanillaEngine> (new QuantLib::FDVanillaEngine ( _process,  _timeSteps,  _gridPoints,  _timeDependent ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (boost::shared_ptr<QuantLib::FDVanillaEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFDVanillaEngine = NULL;
#endif
	_ppFDVanillaEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (QuantLib::FDVanillaEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFDVanillaEngine = NULL;
#endif
	_ppFDVanillaEngine = new boost::shared_ptr<QuantLib::FDVanillaEngine> (&childNative);
    
    _FDVanillaEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (CFDVanillaEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFDVanillaEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFDVanillaEngine = new boost::shared_ptr<QuantLib::FDVanillaEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFDVanillaEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFDVanillaEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (QuantLib::Handle<QuantLib::FDVanillaEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFDVanillaEngine = &childNative;
	_ppFDVanillaEngine = &static_cast<boost::shared_ptr<QuantLib::FDVanillaEngine>>(childNative.currentLink());
    
    _FDVanillaEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (QuantLib::Handle<QuantLib::FDVanillaEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFDVanillaEngine = &childNative;
	_ppFDVanillaEngine = &static_cast<boost::shared_ptr<QuantLib::FDVanillaEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::CFDVanillaEngine (QuantLib::FDVanillaEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFDVanillaEngine = NULL;
#endif
	_ppFDVanillaEngine = new boost::shared_ptr<QuantLib::FDVanillaEngine> (new QuantLib::FDVanillaEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::~CFDVanillaEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFDVanillaEngine != NULL)
    {
	    delete _ppFDVanillaEngine;
        _ppFDVanillaEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::!CFDVanillaEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFDVanillaEngine != NULL)
    {
	    delete _ppFDVanillaEngine;
    }
}
QuantLib::FDVanillaEngine& Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::GetReference ()
{
    if (_ppFDVanillaEngine == NULL) throw gcnew NativeNullException ();
	return **_ppFDVanillaEngine;
}
boost::shared_ptr<QuantLib::FDVanillaEngine>& Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::GetShared ()
{
    if (_ppFDVanillaEngine == NULL) throw gcnew NativeNullException ();
	return *_ppFDVanillaEngine;
}
QuantLib::FDVanillaEngine* Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::GetPointer ()
{
    if (_ppFDVanillaEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppFDVanillaEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FDVanillaEngine>& Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::GetHandle ()
{
	if (_phFDVanillaEngine == NULL)
	{
		_phFDVanillaEngine = new Handle<QuantLib::FDVanillaEngine> (*_ppFDVanillaEngine);
	}
	return *_phFDVanillaEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::HasNative () 
{
	return (_ppFDVanillaEngine != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine::Grid::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppFDVanillaEngine)->grid ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IFDVanillaEngine^ Cephei::QL::Pricingengines::Vanilla::CFDVanillaEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, UInt64 timeSteps, UInt64 gridPoints, Microsoft::FSharp::Core::FSharpOption<Boolean>^ timeDependent)
{
    return gcnew CFDVanillaEngine ( process,  timeSteps,  gridPoints,  timeDependent);
}
